import { createApp, type App as AppType } from 'vue';
import App from './App.vue';
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import VueClipBoard from 'vue-clipboard2'
import pinia from './store'
import I18n from '@/language/index';
import router from './router'
import { renderWithQiankun, qiankunWindow } from "vite-plugin-qiankun/dist/helper"
import type { QiankunProps } from "vite-plugin-qiankun/dist/helper"
import { setToken } from './utils/auth';
import './router/permission';
import './index.css';
import 'virtual:svg-icons-register';

import "./assets/iconfont/iconfont.css";
import "./assets/iconfont/iconfont.js";
// import "./assets/iconfont2/iconfont.css";
// import "./assets/iconfont2/iconfont.js";

import { GlobalStore } from "@/store/modules/global";

// import Loong3dv from "loong3-dv"
import Antd, {message} from 'ant-design-vue';
import { UserStore } from "@/store/modules/user";
// import 'ant-design-vue/dist/antd.less'
import 'ant-design-vue/dist/antd.variable.min.css'

import * as Icons from "@ant-design/icons-vue";
import _,{isArray, isNull,has,isEmpty,isNumber} from "lodash";
import type {responseData} from "@/utils/request";


const icons: any = Icons;

type FormatType = {
  str: (str:string) => string
  response:(res:responseData<any>)=>void
  btn_del:(list:string[],item:any)=>any[]
  btn_disabled:(list:string[],status:0|1)=>any[]
}

const format = {
  str(value:any):string{
    if(isNumber(value)) return value + ''
    if(isNull(value) || isEmpty(value)) return '-'
    return value
  },
  response(res:responseData<any>){
    if(res.code === 200){
      message.success(res.msg)
    }
  },
  btn_del(list:string[],item:any):any[]{
    if(!isArray(list)) return list
    if(!list.includes('delete')) return list

    if(has(item,'children') && !isEmpty(item.children)){
      return list.filter((s)=> s!=='delete')
    }
    return list
  },
  btn_disabled(list:string[],status:0|1):any[]{
    if(!isArray(list)) return list
    if(!list.some((v)=> ['disable','enable'].includes(v))) return list
    if(status === 1) return list.filter((v)=>v !== 'disable')
    return list.filter((v)=>v !== 'enable')
  }
}


let app: AppType<Element>;
if (!qiankunWindow.__POWERED_BY_QIANKUN__) {
  app = createApp(App)
  app.use(pinia)
  app.use(I18n)
  app.use(router)
  app.use(Antd)
  app.use(VueClipBoard)
  // app.use((Loong3dv as any))

  for (const i in icons) {
    app.component(i, icons[i]);
  }

  app.directive('hasPermission', {
    mounted(el, binding) {
      const { value } = binding;
      const userStore = UserStore();
      const f = userStore.validator(value)
      if (!f) {
        el.parentNode && el.parentNode.removeChild(el);
      }
    }
  })


  app.config.globalProperties.$format = format
  app.config.globalProperties.$chain = _.chain

  app.mount('#app')
} else {
  renderWithQiankun({
    mount(props: QiankunProps) {
      setToken(props.token())

      app = createApp(App);
      app.use(pinia)
      app.use(I18n)
      app.use(router)
      app.use(Antd)
      app.use(VueClipBoard)
      // app.use((Loong3dv as any))

      for (const i in icons) {
        app.component(i, icons[i]);
      }

      app.directive('hasPermission', {
        mounted(el, binding) {
          const { value } = binding;
          const userStore = UserStore();
          const f = userStore.validator(value)
          if (!f) {
            el.parentNode && el.parentNode.removeChild(el);
          }
        }
      })

      app.config.globalProperties.$format = format
      app.config.globalProperties.$chain = _.chain

      app.mount(props.container?.querySelector("#app") as any)


      const globalStore= GlobalStore();

      props.onGlobalStateChange((state:any,prev:any)=>{
        console.log('主应用数据发生变化',state,prev)
        globalStore.setMode(state.theme)
        globalStore.updateLanguage(state.locale)
      })

    },
    // eslint-disable-next-line @typescript-eslint/no-empty-function
    bootstrap: function (): void | Promise<void> {},
    unmount: function (): void | Promise<void> {
      // console.log('应用卸载',props);
      app?.unmount();
    },
    update: function (): void | Promise<void> {
      // console.log('更新',props);
    },
  })
}

declare module '@vue/runtime-core'{
  interface ComponentCustomProperties{
    $format:FormatType
  }
}
